<template>
  <a-modal
    v-model="visible"
    :title="disabled ? '编辑' : '新增'"
    @ok="onSubmit"
    @cancel="close"
    width="1000px"
  >
    <a-form-model
      v-if="visible"
      ref="prefixForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <div>{{!showView}}</div>
      <a-form-model-item ref="analysisPath" label="标识路径" prop="analysisPath">
        <a-input
          :disabled="disabled || showView"
          v-model="form.analysisPath"
          placeholder="输入标识路径"
        />
      </a-form-model-item>

<!--      <a-form-model-item ref="templateId" label="模板" prop="templateId">-->
<!--        <a-input-->
<!--          v-model="form.templateId"-->
<!--          placeholder="输入节点名称"-->
<!--        />-->
<!--      </a-form-model-item>-->

      <a-form-model-item ref="data" label="标识数据" prop="data">
        <a-input
          :disabled="showView"
          v-model="form.data"
          placeholder="输入标识数据"
        />
      </a-form-model-item>

      <a-form-model-item ref="dataAttr" label="标识属性" prop="dataAttr">
        <a-select :disabled="showView" v-model="form.dataAttr" placeholder="选择标识属性">
          <a-select-option v-for="item in dataAttrList" :key="item.id" :value="item.id">
            {{ item.label }}
          </a-select-option>
        </a-select>
      </a-form-model-item>

      <a-form-model-item ref="dataType" label="数据类型" prop="dataType">
<!--        <a-input-->
<!--          v-model="form.dataType"-->
<!--          placeholder="输入节点名称"-->
<!--        />-->
        <a-select :disabled="showView" v-model="form.dataType" placeholder="选择数据类型">
          <a-select-option v-for="item in dataTypeList" :key="item.id" :value="item.id">
            {{ item.label }}
          </a-select-option>
        </a-select>
      </a-form-model-item>

    </a-form-model>
  </a-modal>
</template>
<script>
  import {save} from '@/api/identity'

  export default {
  name: 'Edit',
  data: function() {
    return {
      labelCol: {span: 4},
      wrapperCol: {span: 14},
      visible: false,
      disabled: false,
      showView: false,
      form: {
        id: '',
      },
      rules: {
        analysisPath: [
          {required: true, message: '请输入标识路径', trigger: 'blur'}
        ],
        data: [
          {required: true, message: '请输入标识数据', trigger: 'blur'}
        ],
        dataAttr: [
          {required: true, message: '请选择标识属性'}
        ],
        dataType: [
          {required: true, message: '请选择数据类型'}
        ],
      },
      dataAttrList: [
        {id: 0, label: '共有'},
        {id: 1, label: '私有'}
      ],
      dataTypeList: [
        {id: 1, label: '字符串'},
        {id: 2, label: '整型'},
        {id: 3, label: '长整型'},
        {id: 4, label: '浮点型'},
        {id: 5, label: '图片'},
        {id: 6, label: 'URL'},
        {id: 7, label: '引用'},
      ],
    }
  },
  mounted() {

  },
  methods: {
    async initData(obj, showView) {
      this.visible = true
      if (obj.id) {
        this.$nextTick(() => {
          this.form = obj
        })
        this.disabled = true
      } else {
        this.disabled = false
        this.$nextTick(() => {
          this.form = {}
        })
      }
      if (showView) {
        this.showView = true
      } else {
        this.showView = false
      }
    },
    async onSubmit() {
      if (this.showView) {
        this.visible = false
        return
      }
      this.$refs.prefixForm.validate(valid => {
        if (valid) {
          this.save()
        }
      });
    },
    async save(){
      let assignForm = Object.assign({}, this.form);
      if (assignForm.id) {
        assignForm.createDate = '';
      }
      save(assignForm).then(res => {
        if (res.code === '200') {
          this.$message.success('保存成功')
        } else {
          this.$message.error('保存失败')
        }
      }).finally(() => {
        this.close()
      });
    },
    close() {
      this.form = {
        id: '',
      };
      this.visible = false
      this.$emit('ok')
    }
  }
}
</script>
